package com.hz.Tech2Real.yeyouwang.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class loginDao {
	
	private static final String driver = "com.mysql.jdbc.Driver";
	private static final String url = "jdbc:mysql://192.168.50.51:3306/tech2real";
	private static final String user = "root";
	private static final String PASSWORD = "root";
	
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	
	public Map<String, Object> login(String phone, String password){
		
		Map<String, Object> map = null;
		
		StringBuffer sql = new StringBuffer("SELECT phone,`password` FROM uesr_info WHERE phone = ?");
		sql.append(" AND `password` = MD5('");
		sql.append(password);
		sql.append("')");
		System.out.println(sql.toString());
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, PASSWORD);
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, phone);
			rs = ps.executeQuery();
			
			while (rs.next()){
				map=new HashMap<String, Object>();
				map.put("phone", rs.getString("phone"));
				map.put("password", rs.getString("password"));
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		
		return map;
	}

}
